package com.cha.chaApp.vaccination.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cha.chaApp.vaccination.dao.VaccinationDao;
import com.cha.chaApp.vaccination.param.VaccinationParam;
import com.cha.chaApp.vaccination.service.VaccinationService;
import com.cha.chaApp.vaccination.service.VaccinationTypeService;
import com.cha.chaApp.vaccination.vo.VaccinationVO;
import com.cha.chaCommon.core.common.PageParam;
import com.cha.chaCommon.core.entity.hgq.Vaccination;
import com.cha.chaCommon.core.entity.hgq.VaccinationType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VaccinationServiceImpl implements VaccinationService {
    @Resource
    private VaccinationDao vaccinationDao;

    @Resource
    private VaccinationTypeService vaccinationTypeService;

    /**
     * 宝宝id 查宝宝疫苗情况
     *
     * @param id
     * @return
     */
    @Override
    public List<Vaccination> selectAllVaccinationByBabyId(Integer id) {
        //疫苗类型
        List<VaccinationType> vaccinationTypes = vaccinationTypeService.selectAllVaccinationTypeById();

        List<Vaccination> vaccinations = vaccinationDao.selectAllVaccinationByBabyId(id);
        List<Vaccination> vaccinationsList = vaccinations.stream().filter(item -> {
            return item.getBabyId().equals(id);
        }).collect(Collectors.toList());

        List<VaccinationType> collect = null;
        int i = 0;
        for (Vaccination vaccination : vaccinationsList) {
            collect.set(i, (VaccinationType) vaccinationTypes.stream().filter(item -> {
                return item.getVaccinationTypeId().equals(vaccination.getKindId());
            }).findFirst().get());
            i++;
        }

        return vaccinations;
    }

    /**
     * 弃用
     * @param param
     * @param id
     * @return
     */
    @Override
    public List<VaccinationVO> selectAllVaccinationVOByBabyId(PageParam param,Integer id) {

        Page<VaccinationVO> page = new Page<>(param.getPageNum(),param.getPageSize());
        List<VaccinationVO> vaccinationVOS = vaccinationDao.selectAllVaccinationVOByBabyId(id);
        return vaccinationVOS;
    }

    @Override
    public IPage<VaccinationVO> selectOrderPage(PageParam param, Integer babyId) {
        //存宝宝id

        Page<VaccinationVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        page.setOptimizeCountSql(false);
        IPage<VaccinationVO> vaccinationVOIPage = vaccinationDao.selectOrderPage(page, babyId);
        return vaccinationVOIPage;
    }

    @Override
    public void addVaccination(VaccinationParam param) {
        Vaccination vaccination = new Vaccination();
        BeanUtils.copyProperties(param,vaccination);
        vaccinationDao.insert(vaccination);
    }

    @Override
    public void updateVaccination(Vaccination param) {
        vaccinationDao.update(null,new LambdaUpdateWrapper<Vaccination>()
                .set(Vaccination::getHasStarted,param.getHasStarted())
                .eq(Vaccination::getVaccinationStatusId,param.getVaccinationStatusId()));
    }

}
